package com.eboss2.sbs.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.AccountInfoListAdapter;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.enums.EnumConsts;
import com.eboss2.sbs.http.API;
import com.eboss2.sbs.http.HttpCallback;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.AccountInfoEntity;
import com.eboss2.sbs.model.AccountInfoListEntity;
import com.eboss2.sbs.model.ShopCollectionInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.StringUtils;
import com.mytools.tool.views.MyListView;
import com.tool.utils.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AccountInfoActivity extends BaseActivity implements View.OnClickListener {
	private final String TAG = "AccountInfoActivity";

	private MyListView listview;
	private AccountInfoListAdapter adapter;
	private List<AccountInfoEntity> datas;

	private ShopCollectionInfo shopCollectionInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_account_info);
		MyActivityManager.getInstance().addActivity(this);
		initView();

	}

	private void initView() {
		RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "收款账户信息 ");

		datas = new ArrayList<AccountInfoEntity>();
		listview = (MyListView) findViewById(R.id.listview);
		adapter = new AccountInfoListAdapter(this, datas);
		listview.setAdapter(adapter);

		findViewById(R.id.titleback_btn_back).setOnClickListener(this);

		getdata();
	}

	private void getdata() {

		showProgress("加载中...");
		RequestParams params = new RequestParams();
		params.add("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid()+"");
		HttpUtil.get(Config.SBS_COLLECTION_INFO, params, new HttpUtil.RequestListener() {
			@Override
			public void success(String response) {
				disShowProgress();
				try {
					JSONObject result = new JSONObject(response);

					if (!result.optBoolean("status")) {
						ToastUtils.CustomShow(mContext, result.optString("message"));
					}else{
						shopCollectionInfo = new Gson().fromJson(result.optString("data"), new TypeToken<ShopCollectionInfo>(){}.getType());
						if (shopCollectionInfo != null){
							showView(R.id.ly_fuyou_account_infos, true);
							showView(R.id.ly_sqb_account_infos, false);
							setTvText(R.id.tv_member_name, StringUtils.isBlank(shopCollectionInfo.getShopName()) ? "" : shopCollectionInfo.getShopName());
							setTvText(R.id.tv_fy_merchant_no, StringUtils.isBlank(shopCollectionInfo.getScanMerchantId()) ? "" : shopCollectionInfo.getScanMerchantId());
							setTvText(R.id.tv_fy_merchant_name, StringUtils.isBlank(shopCollectionInfo.getScanMerchantName()) ? "" : shopCollectionInfo.getScanMerchantName());
						}


					}
				} catch (Exception e) {
					Log.e(TAG, "doLogin() Exception: " + e.getMessage());
				}


			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});


//		Map<String, Object> params = new HashMap<String, Object>();
////		params.put("sid", mApp.getLoginEntity().getSid());
//		//wcp-2016-11-30
//		TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//		if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
//			// TODO: Consider calling
//			//    ActivityCompat#requestPermissions
//			// here to request the missing permissions, and then overriding
//			//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//			//                                          int[] grantResults)
//			// to handle the case where the user grants the permission. See the documentation
//			// for ActivityCompat#requestPermissions for more details.
//			return;
//		}
//		String imei = tm.getDeviceId();
//		params.put("serialNum", imei);
//		API.http_post(this, API.ACCOUNT_INFO, params, new HttpCallback<AccountInfoListEntity>(this) {
//			@Override
//			public void onSuccess(int statusCode, AccountInfoListEntity data) {
//				if(data!=null){
//					setTvText(R.id.tv_member_name, data.getMemberName());
//					if(data.getScanPayType()== EnumConsts.ScanPayType.SQB.getType()){
//						showView(R.id.ly_sqb_account_infos, true);
//						showView(R.id.ly_fuyou_account_infos, false);
////						setTvText(R.id.tv_terminal_no, LocalParasUtils.getActivateCode(mContext));
////						setTvText(R.id.tv_terminal_name, LocalParasUtils.getTerminalName(mContext));
//						List<AccountInfoEntity> tmp_datas = data.getAccounts();
//						if(tmp_datas!=null&&tmp_datas.size()>0){datas.addAll(tmp_datas);}
//						datas.clear();
//						adapter.notifyDataSetChanged();
//					}
//					else if(data.getScanPayType()== EnumConsts.ScanPayType.FUYOU.getType()){
//						showView(R.id.ly_fuyou_account_infos, true);
//						showView(R.id.ly_sqb_account_infos, false);
//						setTvText(R.id.tv_fy_merchant_no, data.getFyMerchantNo());
//						setTvText(R.id.tv_fy_merchant_name, data.getFyMerchantName());
//					}
//				}
//			}
//			@Override
//			public TypeToken getTypeToken() {
//				return new TypeToken<AccountInfoListEntity>() {};
//			}
//		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
	
			default:break;
		}
	}
	
}
